package com.chenxin.wms.controller;

import com.chenxin.common.annotation.Log;
import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.core.page.TableDataInfo;
import com.chenxin.common.enums.BusinessType;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.common.utils.poi.ExcelUtil;
import com.chenxin.wms.domain.dto.ManualAllotRequestDTO;
import com.chenxin.wms.domain.vo.ObDetailVO;
import com.chenxin.wms.service.IObDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出库明细单Controller
 *
 * @author gry
 * @date 2025-04-15
 */
@RestController
@RequestMapping("/wms/obDetails")
public class ObDetailController extends BaseController {
    @Autowired
    private IObDetailService obDetailService;

    /**
     * 查询出库明细单列表
     */
    @PreAuthorize("@ss.hasPermi('wms:obDetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(ObDetailVO obDetailVo) {
        startPage();
        List<ObDetailVO> list = obDetailService.selectObDetailList(obDetailVo);
        return getDataTable(list);
    }

    /**
     * 导出出库明细单列表
     */
    @PreAuthorize("@ss.hasPermi('wms:obDetails:export')")
    @Log(title = "出库明细单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ObDetailVO obDetailVo) {
        List<ObDetailVO> list = obDetailService.selectObDetailList(obDetailVo);
        ExcelUtil<ObDetailVO> util = new ExcelUtil<>(ObDetailVO.class);
        util.exportExcel(response, list, "出库明细单数据");
    }

    /**
     * 获取出库明细单详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:obDetails:query')")
    @GetMapping(value = {"/", "/{obDetailId}"})
    public AjaxResult getInfo(@PathVariable(value = "obDetailId", required = false) Long obDetailId) {
        return success(obDetailService.selectObDetailById(obDetailId));
    }

    @PreAuthorize("@ss.hasPermi('wms:obDetails:manual-allot')")
    @PostMapping("/obManualAllot")
    public AjaxResult manualAllot(@RequestBody ManualAllotRequestDTO request) {
        if (StringUtils.isNull(request) || request.getStocks().size() == 0) {
            return error("手动分配失败!未分配可用库存");
        }
        return success();
    }
}
